/*
 * MyPropertiesWindow.java
 *
 * Created on May 11, 2000, 12:43 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

//package javaapplication1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 *
 * @author ayman
 * The class MyPropetiesWindow is activated when the user double clicks on any
 * object in the frame
 */

public class MyPropertiesWindow {
    
    private JFrame windowFrame = new JFrame ("properties");
    private MyComponentContainer clickedObject; //object causes action 
    private MyFrame frame; // main panel from main application
    private JScrollPane scrolledPane = new JScrollPane (); // to have a scrolle object
//    private Component [] arrayOfComponents; // components on main panel
    private Vector arrayOfComponentsWithoutPanel ; // components on main panel
    private JPanel panelOnWindow = new JPanel (); // panel on proprties windows
    private FlowLayout flow = new FlowLayout(); // Create a layout manager
    private JComboBox componentsComboBox = new JComboBox(); /*comboBox that has all the 
    componenets name*/
    private JPanel southPanel = new JPanel();
    private JButton applyChanges = new JButton ("Apply");
    private JButton okButton = new JButton ("Ok");
    private JButton cancelButton = new JButton ("Cancel");
    private String componentName[];
    private JTabbedPane mainTabbedPane = new JTabbedPane();
    private JPanel formatePanel = new JPanel();
    private JPanel dataPanel = new JPanel();
    private JPanel eventPanel = new JPanel();
    private JLabel compName = new JLabel("Component Name :");
    private JLabel data = new JLabel("data :");
    private JLabel onClick = new JLabel("Event on click :");
    private Vector infoCompVector;
    private MyComponentInfo info;
    private boolean flagApply=false;

    
    //**************************************************************************
    public MyPropertiesWindow (MyComponentContainer c,MyFrame frame){
       // System.out.println("properties");
        this.frame = frame;
        this.clickedObject = c;
        buildTheWindow();
    }
    
    //**************************************************************************
    /**
     *This method builds the main properties window
     */
    private void buildTheWindow () {
        
        
        /*if (clickedObject instanceof JButton  ) {
            System.out.println("it is button");
        }
        if (clickedObject instanceof JTextArea  ) {
            System.out.println("it is text");
        }*/
        
        
        
        Toolkit kit = Toolkit.getDefaultToolkit ();
        Dimension d = kit.getScreenSize();
        double width = d.getWidth();
        double height= d.getHeight();
        
        //**********************************************************************
        /*
         *Creating the test frame with the getted coordinates
         */
        windowFrame.setLocation ((int)(width/4),(int)(height/4));
        windowFrame.setSize ((int)(300),(int)(250));
        windowFrame.setResizable(true);
        
        //**********************************************************************
        /*
         *Creaeting a combo box that has all the components on the frame
         */
        
        //arrayOfComponents = mainPanel.getComponents();
        //System.out.println("the # of comp is "+this.arrayOfComponents.length);
        /*for (int i=0;i<arrayOfComponents.length;i++)
        {
            //System.out.println("the name is " +arrayOfComponents[i].getName());
            if (!(arrayOfComponents[i] instanceof JPanel)) {
                componentsComboBox.addItem(arrayOfComponents[i].getName());
                arrayOfComponentsWithoutPanel.add( arrayOfComponents[i]);
            }
        }*/
        for (int i=0; i <frame.componentList.size();i++) {
            componentsComboBox.addItem(frame.componentList.get(i).getComponent().getName());
        }
        //arrayOfComponentsWithoutPanel = MyFrame.componentList;
        ActionListnerComboBox newActionHandler = new ActionListnerComboBox();
        componentsComboBox.addActionListener(newActionHandler);
        
        //**********************************************************************
        /**
         * set the select combo box to the clicked compnenet name
         */
        componentsComboBox.setSelectedItem(clickedObject.getComponent().getName());
        
        //**********************************************************************
        //Add label to formate tabe
        // Craeteing a class to obtain all the data about the clicked component
       // MyComponentInfo info = new MyComponentInfo(clickedObject);
        // Get the vector that has the objects that represents the properties
        // of the clicked object
       // infoCompVector=info.getCompoInfo();
       // formatePanel.setLayout( new GridLayout((infoCompVector.size()/2),2) );
       // for (int i=0; i<infoCompVector.size();i++) {
      //      formatePanel.add((Component)infoCompVector.get(i));
      //  }

        
        //**********************************************************************
        //Add label to date tabe
        dataPanel.setLayout( new BorderLayout() );
        dataPanel.add(data,BorderLayout.CENTER);
        
        //**********************************************************************
        //Add label to Event tabe
        eventPanel.setLayout( new BorderLayout() );
        eventPanel.add(onClick,BorderLayout.CENTER);
        
        
        //**********************************************************************
        //adding tabes to tabbed pane
        
        mainTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT  );
        //mainTabbedPane.addTab( " Format ", formatePanel );
	 scrolledPane.setViewportView(formatePanel);
        
        mainTabbedPane.addTab( " Format ", scrolledPane );
       
        mainTabbedPane.addTab( "  Data  ", dataPanel );
	mainTabbedPane.addTab( "  Event ", eventPanel );
	
        
        
        //**********************************************************************
        // Adding ok, Cancel and Apply buttons to the south button
        southPanel.setLayout(flow);
        // Creating a listener
        ControlButtonsHandel controlButtons = new ControlButtonsHandel();
        
        
        //Watch cancel button
        cancelButton.addMouseListener(controlButtons);
        //watch ok button
        okButton.addMouseListener(controlButtons);
        //watch apply button
        applyChanges.addMouseListener(controlButtons);
        
        // setting names for the buttons
        okButton.setName("ok");
        applyChanges.setName("apply");
        applyChanges.setEnabled(false);
        cancelButton.setName("cancel");
        
        // Adding on south panel the buttons
        southPanel.add(okButton);
        southPanel.add(applyChanges);
        southPanel.add(cancelButton);
        
        
        //**********************************************************************
        //addin compnonet on panel
        panelOnWindow.setLayout( new BorderLayout() );
        panelOnWindow.add(componentsComboBox, BorderLayout.NORTH);
        panelOnWindow.add(mainTabbedPane, BorderLayout.CENTER);
        panelOnWindow.add(southPanel, BorderLayout.SOUTH);
        
        //**********************************************************************        
        //Adding compnents to window frame
        windowFrame.add(panelOnWindow);
        windowFrame.setVisible(true);
    }
    
    /**
     *
     * @author ayman
     * The class that extends the interface and act when combobox is used
     */
    class ActionListnerComboBox implements ActionListener{

        /** Creates a new instance of ActionListnerComboBox */
        public ActionListnerComboBox() {
        }

        /**
         * The code that will be run when the user chooses an element in the comboBox
         */
        public void actionPerformed(ActionEvent e) {
            int selectedIndex = componentsComboBox.getSelectedIndex();
            // Remove all the border from all frames
              for (int i =0 ; i < frame.componentList.size();i++) {
                    frame.componentList.get(i).hideBorder();
                }
            // Show the border of the selected Item
           frame.componentList.get(componentsComboBox.getSelectedIndex()).showBorder();
           frame.formatToolBar.setSelectedComponent((String)componentsComboBox.getSelectedItem());
           info = new MyComponentInfo(frame.componentList.get(selectedIndex),windowFrame, frame.getTableName());
            //info = new MyComponentInfo(clickedObject );
            infoCompVector=info.getCompoInfo();
            //System.out.println ("---------------done : "+infoCompVector.size());
            //scrolledPane.setLayout( new ScrollPaneLayout());
            formatePanel.setLayout( new GridLayout((infoCompVector.size()/2),2) );
            //formatePanel.removeAll();
            formatePanel.removeAll();
            for (int i=0; i<infoCompVector.size();i++) {
                formatePanel.add((Component)infoCompVector.get(i));
                ((Component)infoCompVector.get(i)).addMouseListener(new MouseAdapter () {
                    public void mousePressed(MouseEvent e){
                        applyChanges.setEnabled(true);
                    }
                    
                });
                //scrolledPane.add((Component)infoCompVector.get(i));
            }

        }

    }



    /**
     *
     * @author ayman
     * The class that extends the interface and act when Control buttons is pushed
     */
    class ControlButtonsHandel extends MouseAdapter {
        
        
        private String compName;
        private JComponent comp;
        private Vector infoVector = new Vector();
        
        /**
         * Handle the click on (cancel, ok , apply) buttons
         */
        public void mouseClicked(MouseEvent e) {
            
            compName=e.getComponent().getName();
            // User press OK
            if (compName=="ok") {
                // setting the information with close
                try {
                    MyComponentContainer comp = info.getNewInfoAndSave();
                    frame.formatToolBar.refreshObjects();
                    frame.formatToolBar.setSelectedComponent(comp.getName()); 
                    // show border
                    frame.componentList.get(componentsComboBox.getSelectedIndex()).showBorder();
                    windowFrame.dispose();
                }
                catch (Exception exp1) {
                    JOptionPane.showMessageDialog(null,"Please check your entery");
                }
                if (flagApply==false)
                {
                frame.handleUndo();
                }
                
            }
            // User press cancel            
            if (compName=="cancel") {
                windowFrame.dispose();
            }
            // User press Apply            
            if (compName=="apply") {
                // Setting the information without close
                try {
                    MyComponentContainer comp = info.getNewInfoAndSave();
                    frame.formatToolBar.refreshObjects();
                    frame.formatToolBar.setSelectedComponent(comp.getName());
                    // hide border
                    frame.componentList.get(componentsComboBox.getSelectedIndex()).hideBorder();
                    frame.componentList.get(componentsComboBox.getSelectedIndex()).showBorder();
                    try {
                        ((JComponent)e.getSource()).setEnabled(false);
                    }
                    catch (Exception exp) {
                        JOptionPane.showMessageDialog(null,"Please check your entery");
                        System.out.println("error1");
                    }
                }
                catch (Exception ex){
                    JOptionPane.showMessageDialog(null,"Please check your entery");
                    System.out.println(" error");
                }
                frame.handleUndo();
                flagApply=true;
                
                // save the selected component 
                //int selectedIndex = componentsComboBox.getSelectedIndex() ;
                
                //componentsComboBox.insertItemAt(MyFrame.componentList.get(selectedIndex).getComponent().getName(),selectedIndex);
                //componentsComboBox.addItem(MyFrame.componentList.get(selectedIndex).getComponent().getName());
                //componentsComboBox.removeItemAt(selectedIndex);
                //componentsComboBox.setSelectedIndex(selectedIndex);
            }
            
        }
    }
    
}
